package com.hq.shop10search.listener;

import com.hq.commons.constatns.RabbitMQConstatns;
import com.hq.commons.ov.Goods;
import com.hq.shop10search.service.GoodsService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class GoodsExchangeListener {

    @Autowired
    private GoodsService goodsService;

    @RabbitListener(queues = RabbitMQConstatns.GOODS_ES_QUEUE)
    public void addGoodsToEs(Goods goods, Channel channel, Message message) throws IOException {
        try {
//        插入数据到es
            goodsService.addGoodsToEs(goods);
//            收到ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.error("添加到es出现异常",e);
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        }
    }
}
